<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery2.1.1.js"></script>
</head>
<body>
<h2>文件上传</h2>
<form id="f1" enctype="multipart/form-data">
    文件：<input type="file" name="file" multiple/>
    <input type="button" value="上传"/>
</form>
<script>
    async function upload() {
        try {
            // 获取表单数据
            let formData = new FormData($("#f1")[0]);
            // 转换为json数据
            formData = JSON.stringify(formData);
            const response = await fetch("/upload", {
                // 请求方式
                method: "POST",
                // 请求头
                headers: {
                    "Content-Type": "application/json"
                },
                // 发送数据
                body: formData
            });

            // 获取响应数据
            const data = await response.json();

            if (data.code === 200) {
                alert(data.message)
            } else {
                alert("发送失败")
            }
        } catch (error) {
            console.log(error)
        }
    }

    $("#f1 input[type=button]").click(function () {
        upload()
    })
</script>
</body>
</html>